<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;

class MasterStatistic extends BaseModel
{
    protected $table = 'master_statistic';

    const CREATED_AT = null;
    const UPDATED_AT = null;

    protected $dateFormat = 'U';

    protected $fillable = [
        'master_id',
        'points',
        'order_count',
        'income_sum',
        'score_count',
        'score_sum',
        'evaluate_count',
        'evaluate_praise_count',
        'complain_count',
        'is_old'
    ];

    protected $casts = [
        'points' => 'integer',
        'order_count' => 'integer',
        'income_sum' => 'integer',
        'score_count'=>'integer',
        'score_sum'=>'integer',
        'evaluate_count'=>'integer',
        'evaluate_praise_count'=>'integer',
        'complain_count'=>'integer',
    ];

    public function master(){
        return $this->belongsTo(Master::class,'master_id');
    }

    /**
     * 师傅服务打分平均分
     * avg_score
     */
    public function getAvgScoreAttribute()
    {
        if ($this->is_old){
            return sprintf('%.2f',5);
        }
        return sprintf('%.2f',$this->score_count ? $this->score_sum/$this->score_count : 0);
    }

    /**
     * 师傅好评率
     * praise_rate
     */
    public function getPraiseRateAttribute()
    {
        if ($this->is_old){
            return sprintf('%.2f',100);
        }
        return sprintf('%.2f',$this->evaluate_count ? $this->evaluate_praise_count/$this->evaluate_count*100 : 0);
    }

    /**
     * 师傅星级数
     * stars
     */
    public function getStarsAttribute()
    {
        if ($this->is_old){
            $this->points = $this->order_count;
        }
        switch (true){
            case $this->points == 0:
                return 0;
            case $this->points <= 5:
                return 1;
            case $this->points <= 20:
                return 2;
            case $this->points <= 45:
                return 3;
            case $this->points <= 75:
                return 4;
            case $this->points <= 125:
                return 5;
            case $this->points <= 250:
                return 6;
            case $this->points <= 500:
                return 7;
            case $this->points <= 1000:
                return 8;
            case $this->points <= 2500:
                return 9;
            case $this->points <= 5000:
                return 10;
            case $this->points <= 10000:
                return 11;
            case $this->points <= 25000:
                return 12;
            case $this->points <= 50000:
                return 13;
            case $this->points <= 100000:
                return 14;
            case $this->points > 100000:
                return 15;
            default:
                return 0;
        }
        return 0;
    }
}
